package com.wolfeyes.framework.service;

import com.wolfeyes.common.constant.FilmSystemConstants;
import com.wolfeyes.framework.springsecurity.domain.model.CustomizeUserDetails;
import com.wolfeyes.common.redis.RedisCache;
import com.wolfeyes.common.exception.CustomException;
import com.wolfeyes.common.exception.user.CaptchaException;
import com.wolfeyes.common.exception.user.CaptchaExpireException;
import com.wolfeyes.common.exception.user.UserPasswordNotMatchException;
//import com.wolfeyes.common.utils.MessageUtils;
//import cn.andsense.core.manager.AsyncManager;
//import cn.andsense.core.manager.factory.AsyncFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @title 系统登录服务层组件
 * @author yanyljava
 * @create 2020-09-05 13:03
 **/
@Component
public class SysLoginService {

	@Autowired
	private TokenService tokenService;

	@Autowired
	private RedisCache redisCache;

	@Resource
	private AuthenticationManager authenticationManager;

	public String login(String username, String password, String code, String uuid) {
		String verifyKey = FilmSystemConstants.CAPTCHA_CODE_KEY + uuid;
		String captcha = redisCache.getCacheObject(verifyKey);
		redisCache.deleteObject(verifyKey);
		if (captcha == null) {
			//AsyncManager.me().execute(AsyncFactory.recordLoginInfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.jcaptcha.expire")));
			throw new CaptchaExpireException();
		}
		if (!code.equalsIgnoreCase(captcha)) {
			//AsyncManager.me().execute(AsyncFactory.recordLoginInfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.jcaptcha.error")));
			throw new CaptchaException();
		}
		// 用户验证
		Authentication authentication = null;
		try {
			// 该方法会去调用UserDetailsServiceImpl.loadUserByUsername
			authentication = authenticationManager
					.authenticate(new UsernamePasswordAuthenticationToken(username, password));
		} catch (Exception e) {
			if (e instanceof BadCredentialsException)
			{
				//AsyncManager.me().execute(AsyncFactory.recordLoginInfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.password.not.match")));
				throw new UserPasswordNotMatchException();
			}
			else
			{
				//AsyncManager.me().execute(AsyncFactory.recordLoginInfor(username, Constants.LOGIN_FAIL, e.getMessage()));
				throw new CustomException(e.getMessage());
			}
		}
		//AsyncManager.me().execute(AsyncFactory.recordLoginInfor(username, Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success")));
		CustomizeUserDetails customizeUserDetails = (CustomizeUserDetails) authentication.getPrincipal();

		// 生成token
		return tokenService.createToken(customizeUserDetails);
	}

}
